package goodbaby.dklt.activity;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import goodbaby.dklt.R;

/**
 * Created by Jayden on 2016/5/4 11:31.
 *
 * @version V1.0
 * @Title: SurfaceActivity
 * @Description: 视频播放
 */
public class SurfaceActivity extends Activity implements SurfaceHolder.Callback {
    private Button btnBack;
    MediaPlayer player;
    SurfaceView surface;
    SurfaceHolder surfaceHolder;
    private int type = 0;
    private String vedioUrl;

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_surface);
        type = getIntent().getIntExtra("type", 0);
        vedioUrl = getIntent().getStringExtra("vedioUrl");
        surface = (SurfaceView) findViewById(R.id.surface);
        surface.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                SurfaceActivity.this.player.start();
            }
        });
        this.btnBack = (Button) findViewById(R.id.atlas_see_title_back);
        this.btnBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                if (player.isPlaying())
                    player.stop();
                player.release();
                finish();
            }
        });
        surfaceHolder = surface.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setFixedSize(320, 220);
        surfaceHolder.setType(3);
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1, int paramInt2, int paramInt3) {
    }

    public void surfaceCreated(SurfaceHolder paramSurfaceHolder) {
        player = new MediaPlayer();
        player.setAudioStreamType(3);
        player.setDisplay(surfaceHolder);
        try {
            if (type == 0) {
                player.setDataSource(vedioUrl);
            } else {
                Uri localUri = Uri.parse(vedioUrl);
                player.setDataSource(this, localUri);
            }
            player.prepare();
            player.start();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder) {
    }
}
